package android.seabattle;
import java.util.Random;
import android.util.AttributeSet;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

public class Pole extends View
{	
	private float width;    
	private float height;         
	
	public Game game;
	public NetworkGame netGame;
	public Fleet fleet;
	
	private Rect UserField ;
	private Rect EmptyField;
	
	private Bitmap shipimag;
	private Bitmap shipwound; 
	private Bitmap miss;
	private Bitmap open;
	
	Random rand = new Random();
	
	public Pole(Context context,AttributeSet attrs)
	{
		super(context,attrs);
		setFocusable(true);
		setFocusableInTouchMode(true);
		
		if(StartActivity.TypeOfGame == StartActivity.SINGLE)
			this.game = (Game) context;
		else 
			this.netGame = (NetworkGame) context;
	    
		fleet = new Fleet(context);
		
		shipimag = BitmapFactory.decodeResource(getResources(), R.drawable.shipsmal);
		shipwound = BitmapFactory.decodeResource(getResources(), R.drawable.shipwound);
		open = BitmapFactory.decodeResource(getResources(), R.drawable.open);
		miss = BitmapFactory.decodeResource(getResources(), R.drawable.miss);
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
    	width = w/Fleet.SIZE;
	    height = h/Fleet.SIZE;
	    super.onSizeChanged(w, h, oldw, oldh);
	}
	   
	@Override
	protected void onDraw(Canvas canvas) 
	{	
	    Paint hilite = new Paint();
	    hilite.setColor(getResources().getColor(R.color.pole_hilite));
	    hilite.setStrokeWidth(2);
	    
	    Paint ship = new Paint();
 	    ship.setColor(Color.YELLOW);
 	    
	    for(int j = 0;j <= Fleet.SIZE;j++)
	    {
	    	canvas.drawLine(0, j* height , getWidth() , j * height , hilite);
	    	canvas.drawLine(j*width, 0 , j*width , getHeight() ,hilite);
	    }	
	   
	    if(getId() == R.id.user_field)
        {
     	   for(int i = 0;i < Fleet.SIZE;i++)
 	    	  for(int j = 0;j < Fleet.SIZE;j++)
 	    	  {
 	    		  if(Fleet.MyFleet[i][j] == Fleet.SHIP)
 	    		  {
 	    			  UserField =  new Rect((int)(j*width),(int)(i*height),(int)(j*width + width),(int)(i*height + height));
 	    			  canvas.drawBitmap(shipimag, null, UserField,ship);
 	    			  
 	    		  }
 	    		  if(Fleet.MyFleet[i][j] == Fleet.MISS)
 	    		  {
 	    			 UserField = new Rect((int)(j*width),(int)(i*height),(int)(j*width + width),(int)(i*height + height));
 	    			  canvas.drawBitmap(miss,null,UserField,ship);
 	    		  }
 	    		  else if(Fleet.MyFleet[i][j] == Fleet.OPEN)
 	    		  {
 	    			 UserField = new Rect((int)(j*width),(int)(i*height),(int)(j*width + width),(int)(i*height + height));
	    			 canvas.drawBitmap(open,null,UserField,ship);
 	    		  }
 	    		  else if(Fleet.MyFleet[i][j] == Fleet.HIT || Fleet.MyFleet[i][j] == Fleet.KILLED)
 	    		  {
 	    			 UserField = new Rect((int)(j*width),(int)(i*height),(int)(j*width + width),(int)(i*height + height));
	    			 canvas.drawBitmap(shipwound,null,UserField,ship);  
 	    		  }
 	    	  }
        }
	    
	    if(getId() == R.id.enemy_field)
	    {
	    	for(int i = 0;i < Fleet.SIZE;i++)
     	    	for(int j = 0;j < Fleet.SIZE;j++)
     	    	{
     	    		if(Fleet.EnemyFleet[i][j] == Fleet.MISS)
     	    		{
     	    			EmptyField =  new Rect((int)(j*width),(int)(i*height),(int)(j*width + width),(int)(i*height + height));
     	    			canvas.drawBitmap(miss, null, EmptyField,ship);
     	    		}
     	    		else if(Fleet.EnemyFleet[i][j] == Fleet.OPEN)
   	    		  	{
     	    			EmptyField = new Rect((int)(j*width),(int)(i*height),(int)(j*width + width),(int)(i*height + height));
     	    			canvas.drawBitmap(open,null,EmptyField,ship);
   	    		  	}
     	    		else if(Fleet.EnemyFleet[i][j] == Fleet.HIT || Fleet.EnemyFleet[i][j] == Fleet.KILLED)
     	    		{
     	    			EmptyField = new Rect((int)(j*width),(int)(i*height),(int)(j*width + width),(int)(i*height + height));
     	    			canvas.drawBitmap(shipwound,null,EmptyField,ship);  
     	    		}
     	    	}
	    }
	}

	@Override
	public void draw(Canvas canvas) 
	{
		super.draw(canvas);
		
	}
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		int[] buffer = new int[3];
		if (event.getAction() != MotionEvent.ACTION_DOWN)
	    	  return super.onTouchEvent(event);
		if(getId() == R.id.enemy_field)
		{
			if(StartActivity.TypeOfGame == StartActivity.MULTI)
			{
				buffer[0] = (int) (event.getY()/ height);
				buffer[1] = (int) (event.getX()/width);
				buffer[2] = 9;
				netGame.SendCoordinates(buffer);
				invalidate();
			}	
			else 
			{
				fleet.Shot((int)(event.getX() / width),(int) (event.getY() / height));
				invalidate();
			}
		}
		return true;
	}	
	
		
	
}